<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class product extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
        $this->load->model('product/product_model');
        $this->load->model('product/product_provider_model');
        $this->load->model('category/category_model');
        $this->load->model('provider/provider_model');
    }

    public function index() {
    	$this->config->load('site', TRUE);
    	$this->_data['status'] = $this->config->item('status', 'site');
        $clients = $this->product_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['products'] = $clients;
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = $providers;
        $this->load->admintemplate('list', $this->_data);
    }

    public function search() {
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->_data['status'] = $this->config->item('status', 'site');
        $params['product_name'] = $this->input->post('product_name');
        $params['category_id'] = $this->input->post('category_id');
        $params['provider_id'] = $this->input->post('provider_id');
        $clients = $this->product_model->getSearch($params, $sort = 'status', $order = 'asc');
        $this->_data['products'] = $clients;
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = $providers;
        $this->_data['product_name'] = $params['product_name'];
        $this->_data['category_id'] = $params['category_id'];
        $this->_data['provider_id'] = $params['provider_id'];
        $this->load->admintemplate('list', $this->_data);
    }

    public function add() {
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->form_validation->set_rules($this->config->item('product', 'form_validate'));
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = array();
        foreach ($providers as $key => $value) {
        	$this->_data['providers'][$value->id] = $value;
        }
        $this->_data['provider_ids'] = array();
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('add', $this->_data);
        } else {
        	$params['product_name'] = $this->input->post('product_name', TRUE);
            $params['status'] = $this->input->post('status', TRUE);
            $params['category_id'] = $this->input->post('category_id', TRUE);
            $product_id = $this->product_model->insert($params);
            $target['provider_id'] = $this->input->post('provider_id');
            /*if(!$target['provider_id']) {
            	show_error(array('error' => sprintf(lang('lbl_error_part_ctg'), 'Part Category')), 500);
            	exit();
            }*/
            $params_product_provider['product_id'] = $product_id;
            if (isset($target['provider_id'][0])) {
            	foreach ($target['provider_id'] as $key => $value) {
            		$params_product_provider['provider_id'] = $value;
            		$this->product_provider_model->insert($params_product_provider);
            	}
            }
            redirect('product/index');
        }
    }

    public function edit($id) {
    	$this->config->load('site', TRUE);
        $this->_data['product'] = $this->product_model->find_id($id);
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->form_validation->set_rules($this->config->item('product', 'form_validate'));
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $provider_select = $this->product_provider_model->getAllProductProvier($id);
        $this->_data['provider_id_select'] = array();
        $temp = array();
        for ($i = 0; $i < count($provider_select); $i++) {
        	$temp[] = $provider_select[$i]->provider_id;
        	$this->_data['provider_id_select'][$provider_select[$i]->provider_id] = $provider_select[$i];
        }
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = array();
        foreach ($providers as $key => $value) {
        	if (!in_array($value->id, $temp))
        		$this->_data['providers'][$value->id] = $value;
        }
        $this->_data['provider_ids'] = array();
        if ($this->form_validation->run() == FALSE) {
            $this->config->load('site', TRUE);
            $this->load->admintemplate('edit', $this->_data);
        } else {
        	$params['product_name'] = $this->input->post('product_name', TRUE);
            $params['status'] = $this->input->post('status', TRUE);
            $params['category_id'] = $this->input->post('category_id', TRUE);
            $this->product_model->update($id, $params);
            $target['provider_id'] = $this->input->post('provider_id');
            /*if(!$target['provider_id']) {
             show_error(array('error' => sprintf(lang('lbl_error_part_ctg'), 'Part Category')), 500);
            exit();
            }*/
            $this->product_provider_model->deleteRowsByProduct($id);
            $params_product_provider['product_id'] = $id;
            if (isset($target['provider_id'][0])) {
            	foreach ($target['provider_id'] as $key => $value) {
            		$params_product_provider['provider_id'] = $value;
            		$this->product_provider_model->insert($params_product_provider);
            	}
            }
            redirect('product/index');
        }
    }

    public function delete() {
        $id = $this->input->post('id');
        $this->db->trans_start();
        // Delete client
        $delete = (boolean) $this->provider_model->delete($id);
        if ($delete == false) {
            echo 0;
            $this->db->trans_complete();
            die;
        }
        // Delete client's config
        $this->client_config_model->delete($id);
        $this->db->trans_complete();
        if ($this->db->trans_status() !== false) {
            echo 1;
        } else {
            echo 0;
            $this->system_log->log('SQL Failed: ' . $this->db->last_query() . ' - ' . $this->db->_error_message());
        }
        die;
    }

}
